package com.leo.view;

import com.leo.EmailFxApp;
import com.leo.common.JFUtils;
import com.leo.db.DaoMapperProxy;
import com.leo.mapper.UserMapper;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

/**
 * @author wangss
 * @date 2023/02/14 12:26
 * @description 密码修改
 **/
@Slf4j
public class ForgetView {

    public ForgetView() {
        StackPane root = new StackPane();
        VBox vbox = new VBox();
        vbox.getStyleClass().add("main-container");
        vbox.setAlignment(Pos.CENTER);

        Label titleLabel = new Label("你好！请重置密码");
        titleLabel.getStyleClass().add("title-label");

        // 用户名
        HBox userBox = new HBox();
        Label userLabel = new Label();
        TextField userField = new TextField();
        userBox.getChildren().addAll(userLabel, userField);
        HBox.setHgrow(userField, Priority.ALWAYS);
        userBox.getStyleClass().add("user-box");
        userLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.USER));
        userLabel.getStyleClass().add("user-label");
        userField.setPromptText("用户名");
        userField.getStyleClass().add("user-field");
        userField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "base-color;" : "rgb(182,182,182);";
            userBox.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });

        // 邮箱
        HBox emailBox = new HBox();
        Label emailLabel = new Label();
        TextField emailField = new TextField();
        emailBox.getChildren().addAll(emailLabel, emailField);
        HBox.setHgrow(emailField, Priority.ALWAYS);
        emailBox.getStyleClass().add("user-box");
        emailLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.MAIL));
        emailLabel.getStyleClass().add("user-label");
        emailField.setPromptText("邮箱");
        emailField.getStyleClass().add("user-field");
        emailField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "base-color;" : "rgb(182,182,182);";
            emailBox.setStyle(String.format("-fx-border-color:%s", color));
            emailLabel.setStyle(String.format("-fx-border-color:%s", color));
            emailLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });

        // 新密码
        HBox newPswBox = new HBox();
        Label newPswLabel = new Label();
        PasswordField newPswField = new PasswordField();
        newPswBox.getChildren().addAll(newPswLabel, newPswField);
        HBox.setHgrow(newPswField, Priority.ALWAYS);
        newPswBox.getStyleClass().add("psw-box");
        newPswLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.LOCK));
        newPswLabel.getStyleClass().add("psw-label");
        newPswField.setPromptText("新密码");
        newPswField.getStyleClass().add("psw-field");
        newPswField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "base-color;" : "rgb(182,182,182);";
            newPswBox.setStyle(String.format("-fx-border-color:%s", color));
            newPswLabel.setStyle(String.format("-fx-border-color:%s", color));
            newPswLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });

        // 确认密码
        HBox confirmPswBox = new HBox();
        Label confirmPswLabel = new Label();
        PasswordField confirmPswField = new PasswordField();
        confirmPswBox.getChildren().addAll(confirmPswLabel, confirmPswField);
        HBox.setHgrow(confirmPswField, Priority.ALWAYS);
        confirmPswBox.getStyleClass().add("psw-box");
        confirmPswLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.LOCK));
        confirmPswLabel.getStyleClass().add("psw-label");
        confirmPswField.setPromptText("确认密码");
        confirmPswField.getStyleClass().add("psw-field");
        confirmPswField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "base-color;" : "rgb(182,182,182);";
            confirmPswBox.setStyle(String.format("-fx-border-color:%s", color));
            confirmPswLabel.setStyle(String.format("-fx-border-color:%s", color));
            confirmPswLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });

        // 确认修改 、 取消
        HBox confirmButBox = new HBox();
        Button confirmButton = new Button("确认修改");
        Button cancelButton = new Button("取消");
        confirmButBox.getChildren().addAll(confirmButton, cancelButton);
        confirmButBox.setSpacing(20);
        confirmButBox.setAlignment(Pos.CENTER);
        confirmButton.getStyleClass().add("login");
        cancelButton.getStyleClass().add("cancel");

        // 提示语
        Label message = new Label();
        message.getStyleClass().add("message");

        vbox.getChildren().addAll(titleLabel, userBox, emailBox, newPswBox, confirmPswBox, confirmButBox, message);

        root.setPrefWidth(400);
        root.setPrefHeight(500);
        root.getChildren().add(vbox);
        // 拖动
        JFUtils.dragCurrentWindow(vbox);
        Scene scene = new Scene(root);

        Stage stage = new Stage();

        confirmButton.setOnAction(event -> {
            String userFieldText = userField.getText();
            String emailFieldText = emailField.getText();
            String newPswFieldText = newPswField.getText();
            String confirmPswFieldText = confirmPswField.getText();

            if (StringUtils.isBlank(userFieldText)) {
                log.warn("用户名不能为空");
                message.setText("用户名不能为空");
                return;
            }

            if (StringUtils.isBlank(emailFieldText)) {
                log.warn("邮箱不能为空");
                message.setText("邮箱不能为空");
                return;
            }

            if (StringUtils.isAnyBlank(newPswFieldText, confirmPswFieldText)) {
                log.warn("新密码不能为空");
                message.setText("新密码不能为空");
                return;
            }

            if (!StringUtils.equals(newPswFieldText, confirmPswFieldText)) {
                log.warn("两次密码不一致，请检查");
                message.setText("两次密码不一致，请检查");
                return;
            }

            UserMapper userMapper = DaoMapperProxy.getProxyInstance(UserMapper.class);
            int count = userMapper.selectCount(userFieldText, emailFieldText);
            if (count == 0) {
                log.warn("[{},{}] 用户信息不存在，请先进行注册", userFieldText, emailFieldText);
                message.setText("用户信息不存在，请先进行注册");
                return;
            } else if (count > 1) {
                log.warn("[{},{}] 存在多个相同用户无法修改信息", userFieldText, emailFieldText);
                message.setText("存在多个相同用户无法修改信息");
                return;
            }

            int num = userMapper.updatePassword(userFieldText, emailFieldText, newPswFieldText);
            log.warn("[{},{}] 密码修改成功", userFieldText, emailFieldText);
            message.setText("密码修改成功，3秒后自动关闭");

            KeyFrame key = new KeyFrame(Duration.millis(3000), new KeyValue(scene.getRoot().opacityProperty(), 0));
            Timeline timeline = new Timeline(key);
            timeline.setOnFinished(e -> stage.close());
            timeline.play();
        });

        cancelButton.setOnAction(event -> {
            stage.close();
        });

        root.getStylesheets().add(EmailFxApp.class.getResource("css/login.css").toExternalForm());
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setScene(scene);
        stage.showAndWait();
    }
}
